package cn.cmkj.auction.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.Cart;
import cn.cmkj.auction.ui.ShuhuaDetailActivity;
import cn.cmkj.auction.ui.fragment.MainCartFragment;
import cn.cmkj.auction.ui.fragment.MainShuFragment;
import cn.cmkj.auction.widget.ptr.recyclerview.BaseRecycleAdapter;

/**
 * Created by cunguoyao on 2017/9/11.
 */

public class MainCartAdapter extends BaseRecycleAdapter {

    private Context context;
    private List<Cart> mData;
    private ImageOptions imageOptions;
    private MainCartFragment fragment;

    public MainCartAdapter(Context context, MainCartFragment fragment, List<Cart> mData) {
        this.context = context;
        this.mData = mData;
        this.fragment = fragment;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true)//淡入效果
                .setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY).build();
    }

    @Override
    public int onSpan(int position) {
        return 1;
    }

    public Cart getItem(int position) {
        if(mData == null || mData.size() <= 0)return null;
        Cart item = mData.get(position);
        return item;
    }

    @Override
    public int getItemCount() {
        int size = mData != null ? mData.size() : 0;
        return size;
    }

    public void check(boolean checked) {
        if(mData != null && mData.size() > 0) {
            for(Cart zihua : mData) {
                zihua.setChecked(checked);
            }
            notifyDataSetChanged();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_main_cart, parent, false);
        return new MainCartViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Cart item = mData.get(position);
        ((MainCartViewHolder)holder).checkBox.setChecked(item.isChecked());
        x.image().bind(((MainCartViewHolder)holder).image, item.getFaceUrl(), imageOptions);
        ((MainCartViewHolder)holder).textName.setText(item.getName());
        ((MainCartViewHolder)holder).textPrice.setText("￥：" + item.getSalePrice());
        ((MainCartViewHolder)holder).textAges.setText("年代：" + item.getAges());
        ((MainCartViewHolder)holder).textSize.setText("尺寸：" + item.getWidth() + "*" + item.getLength() + "cm");
        ((MainCartViewHolder)holder).checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                item.setChecked(b);
                fragment.onCheck();
            }
        });
        ((MainCartViewHolder)holder).rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ShuhuaDetailActivity.class);
                intent.putExtra("shuhua_id", item.getId());
                context.startActivity(intent);
            }
        });
    }

    class MainCartViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout rootView;
        public CheckBox checkBox;
        public ImageView image;
        public TextView textName;
        public TextView textPrice;
        public TextView textAges;
        public TextView textSize;

        public MainCartViewHolder(View itemView) {
            super(itemView);
            this.rootView = (RelativeLayout)itemView.findViewById(R.id.relative_layout);
            this.checkBox = (CheckBox) itemView.findViewById (R.id.cart_checkbox);
            this.image = (ImageView) itemView.findViewById (R.id.cart_image);
            this.textName = (TextView) itemView.findViewById (R.id.cart_text_name);
            this.textPrice = (TextView) itemView.findViewById (R.id.cart_text_price);
            this.textAges = (TextView) itemView.findViewById (R.id.cart_text_ages);
            this.textSize = (TextView) itemView.findViewById (R.id.cart_text_size);
        }
    }
}
